package com.zhkj.shixi.tvfilemanager.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhkj.shixi.tvfilemanager.Interface.IAdapterNotify;
import com.zhkj.shixi.tvfilemanager.FileManager;
import com.zhkj.shixi.tvfilemanager.R;
import com.zhkj.shixi.tvfilemanager.ViewEntity;
import com.zhkj.shixi.tvfilemanager.view.GrideViewPager;

import java.util.ArrayList;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;

/**
 * Created by nieshuang on 19-8-14.
 */


public class GridViewAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<ViewEntity> mArrayList = null;
    private View mFirstView;
    private IAdapterNotify mIAdapterNotify;

    public GridViewAdapter(Context context, ArrayList<ViewEntity> arrayList){
        this.mContext = context;
        this.mArrayList = arrayList;

    }

    public void setmArrayList(ArrayList<ViewEntity> mArrayList) {
        this.mArrayList = mArrayList;
    }

    public void setIAdapterNotifyCallback(IAdapterNotify iAdapterNotify){
        this.mIAdapterNotify = iAdapterNotify;
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewEntity mBean = (ViewEntity) mArrayList.get(position);
        Log.e("cnk","================1=====position = " + position + " ===========size =" + parent.getChildCount());

        if (convertView == null) {
            Log.i("cnk","================2================");
            convertView = View.inflate(mContext, R.layout.file_gird_item,null);
            viewHolder = new ViewHolder();

            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.grid_useImage);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.grid_useText);
            viewHolder.selectId = (ImageView) convertView.findViewById(R.id.girdSelected);
            Log.d("GridViewAdapter","1");
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
            Log.i("cnk","================2================");
        }

//        viewHolder.imageView.setImageResource(mBean.getmImageId());
//        Log.i("cnk","=======ns =" + mBean.getmFileName());
//        viewHolder.imageView.setImageResource(mBean.getmImageId());
        if (fileManager.getType() == FileManager.IMAGE_TYPE && mBean.getmFile().isFile()){
            viewHolder.imageView.setImageBitmap(FileManager.getImageThumbnail(mBean.getmFile().getAbsolutePath(),FileManager.getScreenWidth(mContext)/6,FileManager.getScreenHeight(mContext)/6));
        }else {
            viewHolder.imageView.setImageResource(mBean.getmImageId());
        }
        viewHolder.textView.setText(mBean.getmFileName());
        if(GrideViewPager.bMap.get(position) == null )
        {
            viewHolder.selectId.setVisibility(View.INVISIBLE);
        }
        else if(GrideViewPager.bMap.get(position) .equals( View.INVISIBLE))
            viewHolder.selectId.setVisibility(View.INVISIBLE);
        else if(GrideViewPager.bMap.get(position) .equals( View.VISIBLE))
            viewHolder.selectId.setVisibility(View.VISIBLE);

        if(position == 0){
            mFirstView = convertView;
        }else if(position == parent.getChildCount()){
            mIAdapterNotify.notifyDataCompleteChanged(mFirstView);
        }
        return convertView;
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
        ImageView selectId;
    }

}